package com.itboye.eweilisten.me.zhanghu;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.me.home.ActivityShopPlay;
import com.itboye.eweilisten.me.login.LoginActivity;
import com.itboye.eweilisten.model.OrderDetalisBean;
import com.itboye.eweilisten.model.ZhangHuBean;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.util.widget.MyListView;
import com.itboye.eweilisten.util.widget.TimeFormat;
import com.itboye.eweilisten.util.widget.XImageLoader;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import static com.itboye.eweilisten.me.zhanghu.ZhangHuFragment.listall;

/**
 * Created by itboye on 2017/3/24.
 * 账户详情
 */

public class ZhangHuDelie extends BaseActivity implements Observer {
    private TextView tvOrderTitle;//标题
    private TextView tvMath;//月份
    private TextView tvpriceOrder;//价格/每一个
    private TextView tvCha;//biaoti
    private TextView tvOrderPrice;//共计多少
    private ImageView orderimgBook, back;//图片
    private TextView tvGongji;
    private TextView tvTitle;
    String OrderCode;
    private UserPresenter presenter;
    private OrderDetalisBean bean;
    private TextView tvCreTime;
    ZhangHuOrderDetileAdapter adapter;
    private MyListView myListViewOrder;
    private TextView orderNumber;
    private String status;
    private RelativeLayout moreDeal;
    private TextView cancel,pay;

    int position;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_order_xinagqing);
        presenter = new UserPresenter(this);
        bean = new OrderDetalisBean();
        OrderCode = getIntent().getStringExtra("orderCode");
        status=getIntent().getStringExtra("status");
        position=getIntent().getIntExtra("position",9999999);

        Log.d("orderCode", OrderCode);
        presenter.onOrderDetails(IsStringSPUtils.getSid(), IsStringSPUtils.IsUid(), OrderCode);
        tvTitle.setText("订单详情");
        orderNumber = (TextView) findViewById(R.id.tv_orderNumber);
        tvCreTime = (TextView) findViewById(R.id.tvCreTi);
        moreDeal=(RelativeLayout)findViewById(R.id.rl_moreDeal);
        cancel=(TextView)findViewById(R.id.tv_cancel);
        cancel.setOnClickListener(this);
        pay=(TextView)findViewById(R.id.tv_pay);
        pay.setOnClickListener(this);
        //如果是待付款项 显示立即支付和取消按钮
        if("2".equals(status)){
            moreDeal.setVisibility(View.VISIBLE);
        }else {
            moreDeal.setVisibility(View.GONE);
        }
        showProgressDialog("数据加载中...", true);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_cancel:
                presenter.onCanlerOrder(IsStringSPUtils.getSid(), IsStringSPUtils.IsUid(),OrderCode);
                break;
            case R.id.tv_pay:
                String price = bean.getItems().get(0).getPrice();
                double dbprice = Double.parseDouble(price);
                Intent intent = new Intent(this, ActivityShopPlay.class);
                intent.putExtra("price", dbprice / 100 + "");
                intent.putExtra("type","chongxin");
                intent.putExtra("order_code",bean.getItems().get(0).getOrder_code());
                for (int i = 0; i < bean.getItems().size(); i++) {
                    intent.putExtra("sku_pkid", bean.getItems().get(i).getPsku_id());
                }
                finish();
                startActivity(intent);
                break;
        }
    }


    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getCode().equals("1111")) {
                Intent intent = new Intent(MyApplication.instance, LoginActivity.class);
                startActivity(intent);
                return;
            }
            if (entity.getEventType() == presenter.OrderDetalis_success) {
                bean = (OrderDetalisBean) entity.getData();
                adapter = new ZhangHuOrderDetileAdapter(bean, this);
                myListViewOrder.setAdapter(adapter);
                tvGongji.setText("共1件商品 实付款:¥" + Double.valueOf(bean.getPrice()) / 100 + "0");
                orderNumber.setText(bean.getOrder_code());
                tvCreTime.setText(TimeFormat.DateFormat(bean.getCreatetime()));
                closeProgressDialog();
            }
            //取消订单
            if (entity.getEventType() == presenter.CanlerOrder_success) {
                MAlert.alert(entity.getData() + "");
                Iterator<ZhangHuBean.InfoList> iterator= listall.iterator();
                while (iterator.hasNext()){
                    ZhangHuBean.InfoList bean=iterator.next();
                    if (bean.getId().equals(listall.get(position).getId())){
                        //  iterator.remove();
                        listall.get(position).setQuery_status("8");
                        break;
                    }
                }
                finish();
                startActivity(new Intent(this,ZhangHuActivity.class));
            }
            if (entity.getEventType() == presenter.CanlerOrder_fail) {
                MAlert.alert(entity.getData());
            }
        }
    }


    public class ZhangHuOrderDetileAdapter extends BaseAdapter {
        OrderDetalisBean bean;
        Context context;

        public ZhangHuOrderDetileAdapter(OrderDetalisBean bean, Context context) {
            this.bean = bean;
            this.context = context;
        }

        @Override
        public int getCount() {
            return bean.getItems() == null ? 0 : bean.getItems().size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHouder houder = null;
            if (convertView == null) {
                houder = new ViewHouder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_order_goodsdiltes, null);
                houder.tvOrderTitle = (TextView) convertView.findViewById(R.id.tvOrderTitle);
                houder.tvpriceOrder = (TextView) convertView.findViewById(R.id.tvpriceOrder);
                houder.tvCha = (TextView) convertView.findViewById(R.id.tvCha);
                houder.tvMath = (TextView) convertView.findViewById(R.id.tvMath);
                houder.orderimgBook = (ImageView) convertView.findViewById(R.id.orderimgBook);
                convertView.setTag(houder);
            } else {
                houder = (ViewHouder) convertView.getTag();
            }
            houder.tvOrderTitle.setText(bean.getItems().get(position).getName());
            houder.tvpriceOrder.setText("¥" + Double.valueOf(bean.getItems().get(position).getPrice()) / 100 + "0");
            houder.tvCha.setText("X" + bean.getItems().get(position).getCount());
            houder.tvMath.setText(bean.getItems().get(position).getSku_desc());
            XImageLoader.load(Const.IMAGE_HEAD + bean.getItems().get(position).getImg(), houder.orderimgBook);

            return convertView;
        }

        public class ViewHouder {
            private TextView tvOrderTitle;
            private TextView tvpriceOrder;//价格/每一个
            private TextView tvCha;//X1
            private TextView tvMath;
            private ImageView orderimgBook;//图片
        }
    }
}
